/**
 * ServerBaseSupport.java created by zhangzhidong 
 * at 下午9:52:20 2016年3月16日
 */
package gs;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangzhidong
 *
 */
public class ServerBaseSupport {
	public static void main(String args[]){
		String prefix = "[" + new SimpleDateFormat("HH:mm:ss").format(new Date())+"]";
		System.out.print(prefix);
//		System.out.println(prefix);
	}
	private static String logPath  = "/Users/zhangzhidong/var/log";
	private static class ServerPrintStream extends PrintStream{
		String prefix = "[" + new SimpleDateFormat("HH:mm:ss").format(new Date())+"]";
		/**
		 * @param fileName
		 * @throws FileNotFoundException
		 */
		public ServerPrintStream(String fileName) throws FileNotFoundException {
			super(fileName);
		}
		public ServerPrintStream(OutputStream out ){
			super(out);
		}
		
		    public void println(String s) {
		        if (s == null) {
		            s = "null";
		        }
		        
		        super.println(prefix + s);
		    }
		
		
	}
	static{
		File dir  = new File(logPath);
		if ( !dir.exists()){
			dir.mkdirs();
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
		String todayString = sdf.format(new Date())+".log"; //2016_2_2
		try{
			File f = new File(logPath+todayString);
			if(!f.exists()){
				if(f.createNewFile()){
					System.setOut(new ServerPrintStream(new FileOutputStream(f)));
				}
			}else{
				System.setOut(new ServerPrintStream(logPath+todayString));
			}
		}catch (Exception e){
			throw new RuntimeException();
		}
	}

}
